//
//  Team9ExpertFaceCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/21.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertFaceCell.h"
#import "Team9JQHeader.h"
#import <MBProgressHUD.h>
#import "Team9SaveModel.h"
#import "DataBaseTool.h"
#import <MBProgressHUD.h>

@interface Team9ExpertFaceCell ()<UIAlertViewDelegate>
@property(nonatomic, strong) Team9BaseLabel *nickLabel;
@property(nonatomic, strong) UIImageView *faceImageView;
@property(nonatomic, strong) UIButton *button;
@property(nonatomic, strong) Team9SaveModel *saver;

@end
@implementation Team9ExpertFaceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView{
    
    Team9BaseView *view = [[Team9BaseView alloc] init];
    [self addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 10, 0, 10));
    }];
    
    
    self.nickLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_nickLabel];
    
    self.faceImageView = [[UIImageView alloc] init];
    [self addSubview:_faceImageView];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setTitle:@"关注" forState:UIControlStateNormal];
    [self.button.titleLabel setFont:[UIFont systemFontOfSize:14 * WIDTH / 375]];
    self.button.layer.cornerRadius = 5;

    self.button.layer.borderWidth = 1;
    self.button.layer.masksToBounds = YES;
    [self addSubview:_button];
    
    [self p_contraints];
}
- (void)changecolor{
    [super changecolor];
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor greenColor];
    } else color = [UIColor orangeColor];
    [self.button setTitleColor:color forState:UIControlStateNormal];
    self.button.layer.borderColor = color.CGColor;
}
- (void)p_contraints{
    [self.faceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.equalTo(self).offset(10);
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.width.height.equalTo(@(40));
    }];
    [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.faceImageView.mas_right).offset(10);
        make.centerY.equalTo(self);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-20);
        make.centerY.equalTo(self);
        make.width.equalTo(self.button.mas_height).multipliedBy(2);
    }];
    self.faceImageView.layer.cornerRadius = 20 * WIDTH / 375;
    self.faceImageView.layer.masksToBounds = YES;
}
- (void)setModel:(Team9ExpertUserModel *)model
{
    if (_model != model) {
        _model = model;
    }
    if (![_model.user_nick isEqualToString:@""] && ![_model isKindOfClass:[NSNull class]] && _model.user_nick) {
        [Team9Tool sd_imageView:self.faceImageView image:self.model.user_face placeholderImage:@"333"];
        [Team9Tool imageView:self.faceImageView Id:self.model.user_id viewController:self.vc barHiden:NO];
        self.nickLabel.text = self.model.user_nick;
        [self.button setHidden:NO];
    } else {
        self.faceImageView.image = [UIImage imageNamed:@"333"];
        self.nickLabel.text = @"未知";
        self.faceImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self.faceImageView addGestureRecognizer:tap];
        [self.button setHidden:YES];
    }
    self.saver = [Team9SaveModel new];
    self.saver.author_id = self.model.user_id;
    self.saver.author_nick = self.model.user_nick;
    self.saver.author_face = self.model.user_face;
    DataBaseTool *dataTool = [DataBaseTool shareDataBaseTool];
    if ([dataTool isHave:self.saver] == InTable) {
        [self.button setTitle:@"已关注" forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
        self.saver.author_type = [dataTool searchType:self.saver];
    } else if ([dataTool isHave:self.saver] == NotInTable){
        [self.button setTitle:@"关注" forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    } else [self.button setTitle:@"错误" forState:UIControlStateNormal];
    
}
- (void)deleteData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] deleteDesigner:self.saver];
    [sender setTitle:@"关注" forState:UIControlStateNormal];
    [sender addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saveData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.labelText = @"正在查询类型,请等待";
    [hud show:YES];
    NSString *url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_page?user_id=%@", self.model.user_id];
    [JQAFNetworkTool getUrl:url success:^(id result) {
        NSString *type = result[@"data"][@"user_home_info"][@"user_info"][@"user_type"];
//        NSLog(@"%@", type);
        self.saver.author_type = type;
        [[DataBaseTool shareDataBaseTool] insertDesigner:self.saver];
        [sender setTitle:@"已关注" forState:UIControlStateNormal];
        [sender addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
        [hud hide:YES];
    }];
  }
- (void)tapAction:(UIGestureRecognizer *)tap{
  if ([UIDevice version] > 8.0) {
    UIAlertController *alertVC = [UIAlertController
        alertControllerWithTitle:@"对不起"
                         message:@"该用户不存在"
                  preferredStyle:UIAlertControllerStyleAlert];
    [self.vc presentViewController:alertVC animated:YES completion:nil];
    dispatch_after(
        dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
        dispatch_get_main_queue(), ^{
          [alertVC dismissViewControllerAnimated:YES
                                      completion:^{
                                      }];
        });
  } else {
    UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle:@"对不起"
                                   message:@"该用户不存在"
                                  delegate:self
                         cancelButtonTitle:nil
                         otherButtonTitles:nil, nil];
    [alert show];
    dispatch_after(
        dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
        dispatch_get_main_queue(), ^{
          [alert dismissWithClickedButtonIndex:0 animated:YES];
        });
  }
}
@end
